<template>
  <div>
    <el-form label-width="100px">
      <el-row type="flex" justify="evenly">
        <el-form-item label="员工名字：">
          <el-input v-model="name" type="text" placeholder="请输入员工名字" @input="onSearch" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-row>
      <el-row style="padding-left: 30px;">
        <el-button type="primary" @click="add">添加员工</el-button>
      </el-row>
    </el-form>

    <el-table
      v-loading="loading"
      style="width: 100%"
      :data="data"
    >

      <el-table-column
        label="序号"
        type="index"
        width="180"
      >
        <template #default="{$index}">
          {{ $index +1+ 10 *(page -1) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="员工名字"
        width="180"
      />
      <el-table-column
        prop="userName"
        label="登录账号"
      />
      <el-table-column
        prop="phonenumber"
        label="联系方式"
      />
      <el-table-column
        prop="roleName"
        label="角色"
      />
      <el-table-column
        prop="status"
        label="状态"
      />
      <el-table-column
        prop="createTime"
        label="添加时间"
      />
      <el-table-column
        prop="demoFlag"
        label="操作"
      >
        <template #default="{row}">
          <el-button type="text" size="small" @click="onSub('edit' ,row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="onSub('del' ,row.id)">删除</el-button>
          <el-button type="text" size="small" @click="onSub('reset' ,row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="float:right">
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20,50,100]"
        layout="total,prev,pager,next,sizes"
        :total="total"
        :page-size="10"
        :size-change="page"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />

    </div>
    <add-emp ref="empRef" :visible.sync="visible" @onSuccess="onLoad" />
  </div>
</template>

<script>

import { getEmployeeAPI } from '@/api/employee'
import addEmp from './components/add-emp.vue'

export default {
  components: { addEmp },
  data() {
    return {
      data: [],
      page: 1,
      pageSize: 10,
      total: 0,
      visible: false,
      name: '',
      loading: false

    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    onSub(type, id) {
      if (type === 'add') {
        // TODO 做新增
        this.visible = true
      }
      if (type === 'edit') {
        // TODO 做编辑
        // 4、ref获取数据
        this.$refs.empRef.onGetDetail(id)
        console.log(id)
        // 提前写入formData.id 防止闪烁
        this.$refs.empRef.formData.id = id
        this.visible = true
      }
      if (type === 'del') {
        // TODO 做删除
        this.$refs.empRef.onDel(id)
      }
    },
    async  onSearch() {
      await this.onLoad()
    },
    add() {
      this.visible = true
    },
    async  onLoad() {
      this.loading = true
      const res = await getEmployeeAPI({ page: this.page, pageSize: this.pageSize })
      this.loading = false
      this.data = res.data.rows
      this.total = res.data.total
    },
    onCurrentChange(page) {
      this.page = page
      this.onLoad()
    },
    onSizeChange(pageSize) {
      this.pageSize = pageSize
      this.onLoad()
      this.page = 1
    }
  }
}
</script>

  <style>

  </style>
